home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / Sample Code / Snippets / Interapplication Communication / SmallDaemon / cSmallDaemon.c next >
Encoding:
C/C++ Source or Header  |  1992-08-27  |  4.0 KB  |  139 lines  |  [TEXT/MPS ]

  1. /*
  2.  *  cSmallDaemon
  3.  *
  4.  *  7/92 Greg Robbins         based on code by C.K. Haun
  5.  *
  6.  *  This is a minimal faceless background application for System 7.
  7.  *
  8.  *  It demonstrates how to install and dispatch Apple events, as well
  9.  *  as the other bare essentials for a faceless background app.
  10.  *
  11.  *  The file type for this application should be 'APPL' if it will be launched
  12.  *  like an application or 'appe' if it will be placed into the Extensions
  13.  *  folder and launched at startup.  'appe' files can also have an INIT resource 
  14.  *  to put up an icon (using ShowInit) at startup.
  15.  */
  16.  
  17. #include "AppleEvents.h"
  18. #include "GestaltEqu.h"
  19.  
  20. #define kSleepMax 216000  // long sleep time (in ticks) to avoid stealing cycles
  21.                           // an app which does something on null events might
  22.                           // sleep less
  23.  
  24. Boolean gAppleEventsFlag, gQuitFlag;
  25. long gSleepVal;
  26.  
  27.  
  28. // Apple event handlers to be installed
  29.  
  30. pascal OSErr DoAEOpenApplication(AppleEvent * theAppleEvent,
  31.                                  AppleEvent * replyAppleEvent, 
  32.                                  long refCon)
  33. {
  34. #pragma unused (theAppleEvent, replyAppleEvent, refCon)
  35.     return noErr;
  36. }
  37.  
  38. pascal OSErr DoAEOpenDocuments(AppleEvent * theAppleEvent,
  39.                                AppleEvent * replyAppleEvent, 
  40.                                long refCon)
  41. {
  42. #pragma unused (theAppleEvent, replyAppleEvent, refCon)
  43.     return errAEEventNotHandled;
  44. }
  45.  
  46. pascal OSErr DoAEPrintDocuments(AppleEvent * theAppleEvent,
  47.                                 AppleEvent * replyAppleEvent, 
  48.                                 long refCon)
  49. {
  50. #pragma unused (theAppleEvent, replyAppleEvent, refCon)
  51.     return errAEEventNotHandled;
  52. }
  53.  
  54. pascal OSErr DoAEQuitApplication(AppleEvent * theAppleEvent,
  55.                                  AppleEvent * replyAppleEvent, 
  56.                                  long refCon)
  57. {
  58. #pragma unused (theAppleEvent, replyAppleEvent, refCon)
  59.     gQuitFlag = true;
  60.     return noErr;
  61. }
  62.  
  63. void InitAppleEventsStuff(void)
  64. // install Apple event handlers
  65. {
  66.     OSErr retCode;
  67.     
  68.     if (gAppleEventsFlag) {
  69.         
  70.         retCode = AEInstallEventHandler(kCoreEventClass, kAEOpenApplication,
  71.                     (EventHandlerProcPtr) DoAEOpenApplication, 0, false);
  72.                                         
  73.         if (retCode == noErr)
  74.             retCode = AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
  75.                     (EventHandlerProcPtr) DoAEOpenDocuments, 0, false);
  76.  
  77.         if (retCode == noErr)
  78.             retCode = AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,
  79.                     (EventHandlerProcPtr) DoAEPrintDocuments, 0, false);
  80.         if (retCode == noErr)
  81.             retCode = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
  82.                     (EventHandlerProcPtr) DoAEQuitApplication, 0, false);
  83.         
  84.         if (retCode != noErr) DebugStr("\pInstall event handler failed");
  85.         // a better way to indicate an error is to post a notification                    
  86.     }
  87. }
  88.  
  89. void DoHighLevelEvent(EventRecord * theEventRecPtr)
  90. // high-level event dispatching
  91. {
  92.     (void) AEProcessAppleEvent(theEventRecPtr);
  93. }
  94.  
  95. main()
  96. {
  97.     OSErr retCode;
  98.     long gestResponse;
  99.     
  100.     EventRecord mainEventRec;
  101.     Boolean eventFlag;
  102.     
  103.     // faceless background apps only get a 2K stack by default.  If necessary,
  104.     // increase the stack size here (by calling GetApplLimit to find the current
  105.     // heap limit, and SetApplLimit to set it to a lower address, thus reserving
  106.     // more space for the stack)
  107.     
  108.     // initialize QuickDraw globals
  109.     
  110.     InitGraf(&qd.thePort);
  111.     
  112.     // initialize application globals
  113.     
  114.     gQuitFlag = false;
  115.     gSleepVal = kSleepMax;
  116.     
  117.     // is the Apple Event Manager available?
  118.     retCode = Gestalt(gestaltAppleEventsAttr, &gestResponse);
  119.     if (retCode == noErr &&
  120.         (gestResponse & (1 << gestaltAppleEventsPresent)) != 0)
  121.         gAppleEventsFlag = true;
  122.     else gAppleEventsFlag = false;
  123.  
  124.     // install Apple event handlers
  125.     InitAppleEventsStuff();
  126.     
  127.     // main event loop
  128.     
  129.     while (!gQuitFlag) {
  130.         eventFlag = WaitNextEvent(everyEvent, &mainEventRec, gSleepVal, nil);
  131.         
  132.         if (mainEventRec.what == kHighLevelEvent)
  133.             DoHighLevelEvent(&mainEventRec);
  134.             
  135.         // during testing, I like to call GetKeys here and check if the CapsLock
  136.         // key is down.  If it is, set gQuitFlag so the program will exit.
  137.     }
  138. }
  139.